home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / rewrite / RemoveRewrit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  3.9 KB  |  139 lines

  1. /*
  2.  * $Header: /private/postgres/src/rewrite/RCS/RemoveRewrite.c,v 2.8 1992/03/25 17:30:18 hong Exp $
  3.  */
  4. #include "access/skey.h"
  5. #include "catalog/pg_rewrite.h"
  6. #include "catalog/catname.h"    /* for RewriteRelationName */
  7. #include "utils/log.h"        /* for elog stuff */
  8. #include "access/tqual.h"    /* 'NowTimeQual' defined here.. */
  9. #include "access/heapam.h"    /* heap AM calls defined here */
  10. #include "access/ftup.h"    /* for FormHeapTuple */
  11. #include "utils/fmgr.h"        /* for CHAR_16_EQ */
  12.  
  13. extern void prs2RemoveRelationLevelLocksOfRule();
  14.  
  15. /* ----------------------------------------------------------------
  16.  *
  17.  * RemoveRewriteRule
  18.  *
  19.  * Delete a rule given its rulename.
  20.  *
  21.  * There are three steps.
  22.  *   1) Find the corresponding tuple in 'pg_rewrite' relation.
  23.  *      Find the rule Id (i.e. the Oid of the tuple) and finally delete
  24.  *      the tuple.
  25.  *   3) Delete the locks from the 'pg_relation' relation.
  26.  *
  27.  *
  28.  * ----------------------------------------------------------------
  29.  */
  30.  
  31. void
  32. RemoveRewriteRule(ruleName)
  33.      Name ruleName;
  34. {
  35.     Relation RewriteRelation    = NULL;
  36.     HeapScanDesc scanDesc    = NULL;
  37.     ScanKey scanKey        = (ScanKey)palloc(sizeof (ScanKeyData));
  38.     HeapTuple tuple        = NULL;
  39.     ObjectId ruleId        = NULL;
  40.     ObjectId eventRelationOid    = NULL;
  41.     Datum eventRelationOidDatum    = NULL;
  42.     Buffer buffer        = NULL;
  43.     Boolean isNull        = false;
  44.  
  45.  
  46.     /*
  47.      * Open the pg_rewrite relation. 
  48.      */
  49.     RewriteRelation = RelationNameOpenHeapRelation(RewriteRelationName);
  50.  
  51.      /*
  52.       * Scan the RuleRelation ('pg_rewrite') until we find a tuple
  53.       */
  54.     ScanKeyEntryInitialize(&scanKey->data[0], 0, Anum_pg_rewrite_rulename,
  55.                    F_CHAR16EQ, NameGetDatum(ruleName));
  56.     scanDesc = RelationBeginHeapScan(RewriteRelation,
  57.                     0, NowTimeQual, 1, scanKey);
  58.  
  59.     tuple = HeapScanGetNextTuple(scanDesc, 0, (Buffer *)NULL);
  60.  
  61.     /*
  62.      * complain if no rule with such name existed
  63.      */
  64.     if (!HeapTupleIsValid(tuple)) {
  65.     RelationCloseHeapRelation(RewriteRelation);
  66.     elog(WARN, "No rule with name = '%s' was found.\n", ruleName);
  67.     }
  68.  
  69.     /*
  70.      * Store the OID of the rule (i.e. the tuple's OID)
  71.      * and the event relation's OID
  72.      */
  73.     ruleId = tuple->t_oid;
  74.     eventRelationOidDatum = HeapTupleGetAttributeValue(
  75.                 tuple,
  76.                 buffer,
  77.                 Anum_pg_rewrite_ev_class,
  78.                 &(RewriteRelation->rd_att),
  79.                 &isNull);
  80.     if (isNull) {
  81.     /* XXX strange!!! */
  82.     elog(WARN, "RemoveRewriteRule: null event target relation!");
  83.     }
  84.     eventRelationOid = DatumGetObjectId(eventRelationOidDatum);
  85.  
  86.     /*
  87.      * Now delete the tuple...
  88.      */
  89.     RelationDeleteHeapTuple(RewriteRelation, &(tuple->t_ctid));
  90.     RelationCloseHeapRelation(RewriteRelation);
  91.     HeapScanEnd(scanDesc);
  92.  
  93.     /*
  94.      * Now delete the relation level locks from the updated relation
  95.      */
  96.     prs2RemoveRelationLevelLocksOfRule(ruleId, eventRelationOid);
  97.  
  98.     elog(DEBUG, "---Rule '%s' deleted.\n", ruleName);
  99.  
  100. }
  101.  
  102. /* temporarily here */
  103. int IsDefinedRewriteRule(ruleName)
  104.      Name ruleName;
  105. {
  106.     Relation RewriteRelation    = NULL;
  107.     HeapScanDesc scanDesc    = NULL;
  108.     ScanKey scanKey        = (ScanKey)palloc(sizeof (ScanKeyData));
  109.     HeapTuple tuple        = NULL;
  110.     ObjectId ruleId        = NULL;
  111.     ObjectId eventRelationOid    = NULL;
  112.     Datum eventRelationOidDatum    = NULL;
  113.     Buffer buffer        = NULL;
  114.     Boolean isNull        = false;
  115.  
  116.  
  117.     /*
  118.      * Open the pg_rewrite relation. 
  119.      */
  120.     RewriteRelation = RelationNameOpenHeapRelation(RewriteRelationName);
  121.  
  122.      /*
  123.       * Scan the RuleRelation ('pg_rewrite') until we find a tuple
  124.       */
  125.     ScanKeyEntryInitialize(&scanKey->data[0], 0, Anum_pg_rewrite_rulename,
  126.                    F_CHAR16EQ, NameGetDatum(ruleName));
  127.     scanDesc = RelationBeginHeapScan(RewriteRelation,
  128.                     0, NowTimeQual, 1, scanKey);
  129.  
  130.     tuple = HeapScanGetNextTuple(scanDesc, 0, (Buffer *)NULL);
  131.  
  132.     /*
  133.      * return whether or not the rewrite rule existed
  134.      */
  135.     RelationCloseHeapRelation(RewriteRelation);
  136.     HeapScanEnd(scanDesc);
  137.     return (HeapTupleIsValid(tuple));
  138. }
  139.